package com.wxapp.common.constant.pojo.vo;

import com.wxapp.common.base.util.jackson.JacksonUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lgl
 * @Description 第三方请求封装类
 * @Datetime 2023/4/7 11:17
 * @Modified By
 */
@Data
@NoArgsConstructor
public class HttpRequestVo<T> {
    /**
     * 返回值类型
     */
    private Class<T> responseType;
    /**
     * 请求url
     */
    private String url;
    /**
     * 请求数据
     */
    private Object data;
    /**
     * 请求头
     */
    private HttpHeaders header;
    /**
     * url 拼接参数 key 参数名称 value 参数值
     */
    private Map<String, Object> urlAppend;
    /**
     * 请求方式
     */
    private HttpMethod method;


    @SafeVarargs
    public HttpRequestVo(String url, HttpMethod method, Object data, Class<T> responseType, Map<String, String>... header) {
        this.url = url;
        this.method = method;
        this.data = data;
        this.responseType=responseType;
        if (header != null && header.length > 0) {
            this.header = new HttpHeaders();
            this.header.setAll(header[0]);
        }
    }

    public MultiValueMap getHeader() {
        if (this.header == null) {
            return new LinkedMultiValueMap<>();
        }
        return this.header;
    }

    public Map<String, Object> getUrlAppend() {
        if (data == null) {
            return new HashMap<>();
        }
        if (data instanceof Map){
            return (Map) data;
        }
        return JacksonUtil.parseObject(data, Map.class);
    }
}
